class TreeNode {
  constructor(val) {
    this.val = val
    this.left = null
    this.right = null
  }
}

function genTree(li) {
  if (li.length === 0) {
    return null
  }
  let t = new TreeNode(li[0])
  let liTree = [t]
  let cur = liTree.shift()
  let setLeft = false
  let setRight = false
  for (let i = 1; i < li.length; i++) {
    if (!setLeft) {
      cur.left = genTreeNode(li[i], liTree)
      setLeft = true
    } else if (!setRight) {
      cur.right = genTreeNode(li[i], liTree)
      setRight = true
    } else {
      cur = liTree.shift()
      cur.left = genTreeNode(li[i], liTree)
      setLeft = true
      setRight = false
    }
  }
  return t
}
function genTreeNode(val, li) {
  if (val === null) {
    return val
  } else {
    let t = new TreeNode(val)
    li.push(t)
    return t
  }
}

function printTree(t, hasNone = true) {
  let li = []
  let liNode = [t]
  while (liNode.length > 0) {
    let tmp = liNode.shift()
    if (tmp) {
      li.push(tmp.val)
      liNode.push(tmp.left)
      liNode.push(tmp.right)
    } else {
      if (hasNone) {
        li.push(null)
      }
    }
  }
  // console.log('li: ', li)
  return li
}

module.exports = { TreeNode, genTree, printTree }
